import * as WebSocket from 'ws';
import { Operater } from '../Operater';
import { Account } from '../schema/AccountSchema';
import { ObjectPool, BaseObject } from '../Pool';
import com = require('com');

export class LoginManager
{
    private static _instance: LoginManager;

    constructor()
    {
        this.init();
    }

    public static getInstance(): LoginManager
    {
        if (this._instance == null)
        {
            this._instance = new LoginManager();
        }
        return this._instance;
    }

    private init():void
    {
        
    }

    public messageParse(ws:WebSocket,msg:com.BaseProto):void
    {
        switch(msg.body_name)
        {
            case com.LoginBody.login:
                this.handleLogin(ws,msg);
                break;
            case com.LoginBody.register:
                this.handleRegister(ws,msg);
                break;
        }
    }

    private handleLogin(ws:WebSocket,login:com.LoginProto):void
    {
        let data = new Account();
        let self = this;
        data.account = login.body.acc;
        Operater.queryByFactor(Operater.AccountEntity,data).then(function(result){
            console.warn('result');
            console.log(result);
            if(result.length == 0)
            {
                //没有这个帐号，暂时直接创建一个
                //正常流程返回没有这个帐号，需要注册
                // data.password = login.body.pwd;
                // self.register(data);
                //帐号不存在
                let base = new com.BaseProto();
                base.seq = login.seq;
                base.code = com.Code.correct;
                ws.send(base.getStr());
            }
            else if(result.length == 1)
            {
                //有这个帐号，存起来
                let base = new com.BaseProto();
                base.seq = login.seq;
                base.code = com.Code.correct;
                base.body = result[0];
                base.head_name = com.HeadName.login;
                base.body_name = com.LoginBody.login;
                ws.send(base.getStr());
                let obj = new BaseObject();
                // obj._id = result[0]._id;
                // obj.acc = result[0].account;
                // ObjectPool.getInstance().addObj()
            }
            else if(result.length > 1)
            {
                //帐号整重了，需要紧急处理
                let base = new com.BaseProto();
                base.seq = login.seq;
                base.code = com.Code.server;
                ws.send(base.getStr());
            }
        }).catch(function(err){
            //查询出错，可能是各种问题
            console.warn('LoginManager.handleLogin err:',err);
        });
    }

    /**处理注册 */
    private handleRegister(ws:WebSocket,register:com.RegisterProto):void
    {
        let data = new Account();
        data.account = register.body.acc;
        data.password = register.body.pwd;
        data.email = register.body.email;
        data.firstName = register.body.firstName;
        data.lastName = register.body.lastName;
        //默认是0
        data.lv = register.body.lv?register.body.lv:0;
        Operater.addDoc(Operater.AccountEntity,data).then(function(result){
            console.log('LoginManager.register result:',result);
            let base = new com.BaseProto();
            base.seq = register.seq;
            base.code = com.Code.correct;
            base.body = result;
            base.head_name = com.HeadName.login;
            base.body_name = com.LoginBody.register;
            ws.send(base.getStr());
            //将register协议返回回去
        }).catch(function(err){
            console.warn('LoginManager.handleRegister err:',err);
        });
    }

    /**基础帐号创建 */
    private register(acc:Account):void
    {
        Operater.addDoc(Operater.AccountEntity,acc).then(function(result){
            console.log('LoginManager.register result:',result);
            //成功需要返回给账号服

        }).catch(function(err){
            console.warn('LoginManager.register err:',err);
        });
    }
}